//
//  UserDefaultsUtils.m
//  butterfly
//
//  Created by Mudmen on 2021/1/16.
//

#import "UserDefaultsUtils.h"

@implementation UserDefaultsUtils

+ (void)saveValue:(id) value forKey:(NSString *)key{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:value forKey:key];
    [userDefaults synchronize];
}

+ (id)valueWithKey:(NSString *)key{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:key];
}

+ (void)removeWithKey:(NSString *)key {
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:key];
}

+ (BOOL)boolValueWithKey:(NSString *)key
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults boolForKey:key];
}

+ (void)saveBoolValue:(BOOL)value withKey:(NSString *)key
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:value forKey:key];
    [userDefaults synchronize];
}

+ (void)print{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults dictionaryRepresentation];
    NSLog(@"%@",dic);
}

//检测几天内一次
+ (BOOL)saveCheckDayTime:(NSInteger)day keyNst:(NSString *)keyNst{
    if ([UserDefaultsUtils valueWithKey:keyNst]) {
        NSDate *nowTime = [NSDate date];
        NSDate *ageDate = (NSDate *)[UserDefaultsUtils valueWithKey:keyNst];
        NSTimeInterval delta = [nowTime timeIntervalSinceDate:ageDate];
        if (delta > day*60*60*24) {
            [UserDefaultsUtils removeWithKey:keyNst];
            [UserDefaultsUtils saveValue:nowTime forKey:keyNst];
            return YES;
        }else{
            return NO;
        }
    }
    NSDate *nowTime = [NSDate date];
    [UserDefaultsUtils saveValue:nowTime forKey:keyNst];
    return YES;
}

@end
